package com.sky.aspection;

import com.sky.annotation.AutoFile;
import com.sky.constant.AutoFillConstant;
import com.sky.constant.JwtClaimsConstant;
import com.sky.context.ThreadLocalUtil;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class AutoFileAspection {

    // 切点
    @Pointcut("execution(* com.sky.mapper.*.*(..))&&@annotation(com.sky.annotation.AutoFile)")
    public void AutoFilePointcut() {
    }

    // 通知底层是 adviser
    @Before("AutoFilePointcut()")
    public void Autofile(JoinPoint joinPoint) throws InvocationTargetException, IllegalAccessException {
        // 获取到当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFile annotation = signature.getMethod().getAnnotation(AutoFile.class);
        OperationType operationType = annotation.value();
        // 获取到当前被拦截的方法的参数 -- 实体对象
        Object[] entities = joinPoint.getArgs();

        if (entities == null || entities.length == 0) {
            return;
        }
        Object entity = entities[0];
        // 准备赋值的数据
        Map<String, Object> parse = ThreadLocalUtil.get();
        Integer userId = (Integer) parse.get(JwtClaimsConstant.USER_ID);
        // 根据当前不同的操作类型，为对应的属性通过反射来赋值
        if (operationType == OperationType.INSERT) {
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method createUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method updateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setCreateTime.invoke(entity, LocalDateTime.now());
                updateTime.invoke(entity, LocalDateTime.now());
                createUser.invoke(entity, (Long.valueOf(userId)));
                updateUser.invoke(entity, (Long.valueOf(userId)));
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        } else if (operationType == OperationType.UPDATE) {
            try {
                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method updateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                updateTime.invoke(entity, LocalDateTime.now());
                updateUser.invoke(entity, (Long.valueOf(userId)));
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
    }
}